$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'systemConfig/systemconfig/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true , hidden:true },
			{ label: '参数名字', name: 'paramName', index: 'param_name', width: 80 }, 			
			{ label: '参数类型', name: 'paramType', index: 'param_type', width: 40 },
			{ label: '参数值', name: 'paramValue', index: 'param_value', width: 120 },
			{ label: '描述', name: 'paramDesc', index: 'param_desc', width: 80 }	,
            { label: '编辑', name: 'edit', index: 'edit', width: 30 ,align: 'center',
                formatter:function(value, options, row){
                    return '<a href="javascript:void(0);" onclick="return vm.update(' + row.id + ')" >编辑</a>';
                }
            }
        ],
		viewrecords: true,
        height: 700,
        rowNum: 25,
		rowList : [25,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            paramName:""
		},
		showList: true,
        showListInfo: false,
		title: null,
		systemConfig: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.showListInfo= true;
			vm.title = "新增";
			vm.systemConfig = {};
		},
		update: function (id) {
			vm.showList = false;
			vm.showListInfo = true;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            if (!vm.systemConfig.paramName) {
                alert("请输入参数名字");
                return ;
            }
            if (!vm.systemConfig.paramType) {
                alert("请输入参数类型");
                return ;
            }
            if (!vm.systemConfig.paramValue) {
                alert("请输入参数值");
                return ;
            }
			var url = vm.systemConfig.id == null ? "systemConfig/systemconfig/save" : "systemConfig/systemconfig/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.systemConfig),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "systemConfig/systemconfig/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "systemConfig/systemconfig/info/"+id, function(r){
                vm.systemConfig = r.systemConfig;
            });
		},
		reload: function (event) {
			vm.showList = true;
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'paramName': vm.q.paramName.trim()
                },
                page:1
            }).trigger("reloadGrid");
		},
        reset: function (event) {
            vm.q.paramName = '';
            vm.reload();
        }
	}
});